﻿/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2017-8-8
 * Time: 8:39
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Win32;

namespace ProxyEditor
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class Window1 : Window
	{
		public Window1()
		{
			InitializeComponent();
			ReadReg();
//			
//			Binding binding=new Binding();
//			binding.Source=po;
//			binding.Path=new PropertyPath("ProxyIP");
//			this.tbIP.SetBinding(TextBox.TextProperty,binding);
//			
//			Binding binding_port=new Binding();
//			binding_port.Source=po;
//			binding_port.Path=new PropertyPath("ProxyPort");
//			this.tbPort.SetBinding(TextBox.TextProperty,binding_port);
//			
//			Binding binding_enable=new Binding();
//			binding_enable.Source=po;
//			binding_enable.Path=new PropertyPath("Enabled");
//			binding_enable.Converter = new EnableConvert();
//			
//			this.btnEnable.SetBinding(Button.ContentProperty,binding_enable);
			
			
		}
		
		ProxyObj po=new ProxyObj();
			
			
		public void ReadReg(){
			using(RegistryKey regKey=Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings")){
			
				
				string s1=regKey.GetValue("ProxyEnable").ToString();
				int enable=0;
				if(!String.IsNullOrEmpty(s1))
					enable=Int32.Parse(s1);
				
				string pip = regKey.GetValue("ProxyServer").ToString();
				
				string[] x=pip.Split(new char[]{':'});
				
				po.Enabled=enable==1;
				po.ProxyPort=x[1];
				po.ProxyIP=x[0];
			}
		}
		
		void WriteReg(){
			using(RegistryKey regKey=Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings")){
			
				regKey.SetValue("ProxyEnable",po.Enabled?1:0);
				
				if(!String.IsNullOrEmpty(po.ProxyIP)){
					StringBuilder sb=new StringBuilder();
					
					sb.Append(po.ProxyIP.Trim());
					
					if(!String.IsNullOrEmpty(po.ProxyPort)){
						sb.Append(":");
						sb.Append(po.ProxyPort.Trim());
					}
					
					regKey.SetValue("ProxyServer",sb.ToString());
				}
			}	
		}
		void window1_Loaded(object sender, RoutedEventArgs e)
		{
			tbIP.DataContext=po;
			tbPort.DataContext=po;
			
			btnEnable.DataContext = po;
		}
		void button1_Click(object sender, RoutedEventArgs e)
		{
			//throw new NotImplementedException();
			po.Enabled=!po.Enabled;
		}
		
		
		void Window1_Closed(object sender, EventArgs e)
		{
			WriteReg();
		}
		void BtnEnable_Checked(object sender, RoutedEventArgs e)
		{
			po.Enabled=true;
		}
		void BtnEnable_UnChecked(object sender, RoutedEventArgs e)
		{
			po.Enabled=false;
		}
		void Button_Click(object sender, RoutedEventArgs e)
		{
			tbShow.Text=po.Enabled?"是":"否";
		}
	}
}